package com.hotacorp.opencloud.flowservice.controller;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hotacorp.opencloud.common.commdata.Result;
import com.hotacorp.opencloud.common.log.annotation.LogAnnotation;
import com.hotacorp.opencloud.common.mybatisplus.PageData;
import com.hotacorp.opencloud.flowservice.dto.ProcessInstanceRunInfo;
import com.hotacorp.opencloud.flowservice.dto.ProcessRequest;
import com.hotacorp.opencloud.flowservice.dto.TaskRequest;
import com.hotacorp.opencloud.flowservice.service.ProcessHandler;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 流程审批管理
 * @author lwg
 *
 */
@RestController
@RequestMapping("/process")
@Api(tags = "流程运行管理API")
public class ProcessController extends AbstractController {
	@Autowired
	private ProcessHandler processHandler;
	
	@PostMapping(value = "/start")
	@ApiOperation(value = "开始流程")
    @LogAnnotation(module = "流程")
	public Result<?> deploy(@RequestBody ProcessRequest request) throws Exception {
		String tenantid = String.valueOf(getTenantid());
		String username = getUsername();
		String id = processHandler.startProcess(tenantid, username, request);
		if (StringUtils.isBlank(id)) {
			return Result.failed("没有找到流程");
		} else {
			return Result.succeed("", id);
		}
	}

	@PostMapping(value = "/approve")
	@ApiOperation(value = "审批流程")
    @LogAnnotation(module = "流程")
	public Result<?> approve(@RequestBody TaskRequest request) throws Exception {
		String tenantid = String.valueOf(getTenantid());
		String username = getUsername();
		processHandler.approveProcess(tenantid, username, request);
		return Result.succeed();
	}
	
	@GetMapping(value = "/status")
	@ApiOperation(value = "取流程状态码")
    @LogAnnotation(module = "流程")
	public Result<?> processstatus(@RequestParam String processinstanceid) {
		String tenantid = String.valueOf(getTenantid());
		String username = getUsername();
		int ret = processHandler.getProcessStatus(tenantid, username, processinstanceid);
		return Result.succeed("", ret);
	}
	
	@GetMapping(value = "/terminate")
	@ApiOperation(value = "终止流程")
    @LogAnnotation(module = "流程")
	public Result<?> terminateprocess(@RequestParam String processinstanceid) {
		String tenantid = String.valueOf(getTenantid());
		String username = getUsername();
		boolean ret = processHandler.terminateProcess(tenantid, username, processinstanceid);
		if (ret) {
			return Result.succeed();
		} else {
			return Result.failed("没有找到流程");
		}
	}

	@GetMapping(value = "/suspend")
	@ApiOperation(value = "挂起流程")
    @LogAnnotation(module = "流程")
	public Result<?> suspendprocess(@RequestParam String processinstanceid) {
		String tenantid = String.valueOf(getTenantid());
		String username = getUsername();
		boolean ret = processHandler.suspendProcess(tenantid, username, processinstanceid);
		if (ret) {
			return Result.succeed();
		} else {
			return Result.failed("没有找到流程");
		}
	}

	@GetMapping(value = "/active")
	@ApiOperation(value = "激活流程")
    @LogAnnotation(module = "流程")
	public Result<?> activeprocess(@RequestParam String processinstanceid) {
		String tenantid = String.valueOf(getTenantid());
		String username = getUsername();
		boolean ret = processHandler.activeProcess(tenantid, username, processinstanceid);
		if (ret) {
			return Result.succeed();
		} else {
			return Result.failed("没有找到流程");
		}
	}

	@GetMapping(value = "/diagram")
	@ApiOperation(value = "取流程状态图")
    @LogAnnotation(module = "流程")
	public Result<?> processdiagram(@RequestParam String processinstanceid) throws Exception {
		String tenantid = String.valueOf(getTenantid());
		String username = getUsername();
		ProcessInstanceRunInfo ret = processHandler.getProcessRunInfo(tenantid, username, processinstanceid);
		if (ret != null) {
			return Result.succeed("", ret);
		} else {
			return Result.failed("取流程图失败");
		}
	}

	@GetMapping(value = "/todo")
	@ApiOperation(value = "取待办任务")
    @LogAnnotation(module = "流程")
	public Result<?> todotask(@RequestParam Map<String, Object> params) {
		String tenantid = String.valueOf(getTenantid());
		String username = getUsername();
		PageData ret = processHandler.needDeal(tenantid, username, params);
		return Result.succeed("", ret);
	}

}
